package com.matrix.draft.helloworld.sys;

import com.github.kwhat.jnativehook.GlobalScreen;
import com.matrix.draft.util.OptimizedJtsUtil;
import org.locationtech.jts.geom.Coordinate;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class TestMouseMoving {

    public static DoMovingThread  doMovingThread = null;

    public static void main(String[] args) throws Exception {
        // for (int i = 0; i < 5; i++) {
        //     int randomX = RandomUtil.randomInt(0, 2048);
        //     int randomY = RandomUtil.randomInt(0, 2048);
        //     smartRobot.mouseMove(randomX,randomY);
        //     Thread.sleep(1000);
        // }

        // org.locationtech.jts.geom.Polygon polygon = OptimizedJtsUtil.createCircle2(1024D, 500D, 200, 36);
        // for (int i = 0; i < 5; i++) {
        //     for (Coordinate coordinate : polygon.getCoordinates()) {
        //         smartRobot.mouseMove((int) coordinate.x, (int) coordinate.y);
        //     }
        // }

        SmartRobot smartRobot = new SmartRobot();
        DoMovingThread doMovingThread = new DoMovingThread(smartRobot);
        TestMouseMoving.doMovingThread = doMovingThread;
        doMovingThread.start();

        GlobalScreen.registerNativeHook();
        GlobalScreen.addNativeMouseListener(new MyNativeMouseRightPressListener(smartRobot));
        GlobalScreen.addNativeMouseListener(new MyNativeMouseRightReleaseListener(smartRobot));

        Thread.sleep(60000);
        GlobalScreen.unregisterNativeHook();
    }

    private Robot robot;
    private boolean isStop = false;

    public TestMouseMoving() throws AWTException {
        try {
            ControllerFrame frame = new ControllerFrame("Prevent Locking");
            frame.setVisible(true);
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        int x;
        int y;
        Random random = new Random();
        while (!isStop) {
            //随机生成坐标。
            x = random.nextInt(1000);
            y = random.nextInt(1000);
            //开始移动
            robot.mouseMove(x, y);
            //每5秒移动一次
            robot.delay(5000);
        }
    }

    /**
     * GUI Frame 生成一个button，控制程序
     *
     * @author max
     */
    private class ControllerFrame extends JFrame {
        private static final long serialVersionUID = 1L;

        private JButton close = new JButton("close");

        public ControllerFrame(String title) {
            this();
            setTitle(title);
        }

        public ControllerFrame() {
            setLayout(new FlowLayout(FlowLayout.LEADING));
            setSize(100, 100);
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);

            Dimension preferredSize = new Dimension(100, 60);
            Font font = new Font("", 1, 14);

            //设置button 大小，文字等属性
            close.setPreferredSize(preferredSize);
            close.setFont(font);
            close.setBorderPainted(true);
            close.setFocusable(false);

            add(close);

            //点击button后，程序终止。
            close.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    isStop = true;
                    dispose();
                }
            });

        }

    }

}
